package com.frikinet.tools.gui;

import java.awt.event.ActionListener;
import java.io.FileInputStream;
import java.io.InputStream;

import javax.swing.JMenuBar;

import org.apache.commons.digester.Digester;

import com.frikinet.tools.i18n.FI18N;

public class FMenuBuilder
{
	private FI18N i18n = null;
	private ActionListener listener = null;
	
	public FMenuBuilder(ActionListener listener, FI18N i18n)
	{
		this.listener = listener;
		this.i18n = i18n;
	}
	
	public <T> T build(String xml, Class<T> clazz)
	{
		try
		{
			return build(new FileInputStream(xml), clazz);
		}
		catch(Exception e)
		{
			e.printStackTrace();
			return null;
		}
	}
	
	@SuppressWarnings("unchecked")
	public <T> T build(InputStream stream, Class<T> clazz)
	{
		Digester digester = getDigester();
		T bar = null;
		
		try
		{
			bar = (T)digester.parse(stream);
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		
		return bar;
	}
	
	@SuppressWarnings({ "rawtypes" })
	private Digester getDigester()
	{
		Digester digester = new Digester();
		
		Class[] paramFI18N = {FI18N.class};
		Class[] paramActionListener = {ActionListener.class};
		digester.addObjectCreate("menubar", FMenuBar.class);
		digester.addCallMethod("menubar", "setI18N", 1, paramFI18N);
		digester.addObjectParam("menubar", 0, i18n);
		digester.addObjectCreate("popup", FPopupMenu.class);
		digester.addCallMethod("popup", "setI18N", 1, paramFI18N);
		digester.addObjectParam("popup", 0, i18n);
		digester.addObjectCreate("*/menu", FMenu.class);
		digester.addSetProperties("*/menu", "id", "id");
		digester.addCallMethod("*/menu", "setI18N", 1, paramFI18N);
		digester.addObjectParam("*/menu", 0, i18n);
		digester.addBeanPropertySetter("*/menu/name","text");
		digester.addCallMethod("*/menu/separator", "addSeparator");
		digester.addSetNext("*/menu", "add");
		digester.addObjectCreate("*/menuitem", FMenuItem.class);
		digester.addSetProperties("*/menuitem", "id", "id");
		digester.addCallMethod("*/menuitem", "setI18N", 1, paramFI18N);
		digester.addObjectParam("*/menuitem", 0, i18n);
		digester.addCallMethod("*/menuitem", "addActionListener", 1, paramActionListener);
		digester.addObjectParam("*/menuitem", 0, listener);
		digester.addBeanPropertySetter("*/menuitem/name", "text");
		digester.addBeanPropertySetter("*/menuitem/actionCommand", "actionCommand");
		digester.addBeanPropertySetter("*/menuitem/accelerator", "acceleratorString");
		digester.addSetNext("*/menuitem", "add");
		digester.addObjectCreate("*/checkmenuitem", FCheckBoxMenuItem.class);
		digester.addCallMethod("*/checkmenuitem", "setI18N", 1, paramFI18N);
		digester.addObjectParam("*/checkmenuitem", 0, i18n);
		digester.addCallMethod("*/checkmenuitem", "addActionListener", 1, paramActionListener);
		digester.addObjectParam("*/checkmenuitem", 0, listener);
		digester.addSetProperties("*/checkmenuitem", "selected", "state");
		digester.addSetProperties("*/checkmenuitem", "id", "id");
		digester.addBeanPropertySetter("*/checkmenuitem/name", "text");
		digester.addBeanPropertySetter("*/checkmenuitem/actionCommand", "actionCommand");
		digester.addBeanPropertySetter("*/checkmenuitem/accelerator", "acceleratorString");
		digester.addSetNext("*/checkmenuitem", "add");
		digester.addObjectCreate("*/radiomenuitem", FRadioButtonMenuItem.class);
		digester.addCallMethod("*/radiomenuitem", "setI18N", 1, paramFI18N);
		digester.addObjectParam("*/radiomenuitem", 0, i18n);
		digester.addCallMethod("*/radiomenuitem", "addActionListener", 1, paramActionListener);
		digester.addObjectParam("*/radiomenuitem", 0, listener);
		digester.addSetProperties("*/radiomenuitem", "selected", "selected");
		digester.addSetProperties("*/radiomenuitem", "group", "group");
		digester.addSetProperties("*/radiomenuitem", "id", "id");
		digester.addBeanPropertySetter("*/radiomenuitem/name", "text");
		digester.addBeanPropertySetter("*/radiomenuitem/actionCommand", "actionCommand");
		digester.addBeanPropertySetter("*/radiomenuitem/accelerator", "acceleratorString");
		digester.addSetNext("*/radiomenuitem", "add");
		
		return digester;
	}	
	
	public static void main(String args[])
	{
		FMenuBuilder builder = new FMenuBuilder(null,null);
		JMenuBar bar = builder.build("test.xml",FMenuBar.class);
		System.out.println("Bar = "+bar.getComponents()[0]);
	}
}
